package com.kaiyiweitong.tongxing.activity.my.person_info;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.kaiyiweitong.tongxing.R;
import com.kaiyiweitong.tongxing.activity.my.PersonInfoActivity;
import com.kaiyiweitong.tongxing.base.BaseActivity;
import com.kaiyiweitong.tongxing.util.MyTextUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/19 0019.
 */

public class SetNicknameActivity extends BaseActivity implements TextWatcher {
    @Bind(R.id.nickname)
    EditText mNickname;
    @Bind(R.id.clear)
    ImageView mClear;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_nickname);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        mNickname.addTextChangedListener(this);
        String nickname = getIntent().getStringExtra("nickname");
        if (nickname != null && !nickname.equals("")) {
            mNickname.setText(nickname);
            mClear.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.clear, R.id.finish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.clear:
                mNickname.setText("");
                break;
            case R.id.finish:
                Intent intent = new Intent(this, PersonInfoActivity.class);
                intent.putExtra("nickname", MyTextUtils.checkTextContent(mNickname));
                setIntent(intent);
                finish();
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (MyTextUtils.checkTextContent(mNickname).equals("")) {
            mClear.setVisibility(View.INVISIBLE);
        } else {
            mClear.setVisibility(View.VISIBLE);
        }
    }
}
